package com.zy.utils

import com.zy.utils.DataBuildUtils
import java.util.*

object DataBuildUtils {
    private const val SIZE = 10
    private const val RANGE = 90

    //10-99
    val randomSortArray: IntArray
        get() = getRandomSortArray(SIZE)

    fun getRandomSortArray(size: Int): IntArray {
        val random = Random()
        val list = IntArray(size)
        val hashSet = HashSet<Int>()
        for (index in 0 until size) {
            var num = 0
            do {
                num = random.nextInt(RANGE) + 10
            } while (hashSet.contains(num))
            hashSet.add(num)
            list[index] = num
        }
        return list
    }

    @JvmStatic
    fun getRandomSortArray(size: Int, range: Int): IntArray {
        var d = range.toFloat() / size.toFloat()
        if (d == 0f) {
            d = 1f
        }
        val list = IntArray(size)
        var base = 1f
        for (index in 0 until size) {
            list[index] = base.toInt()
            base += d
        }
        getRandomSortArrayV2(list)
        return list
    }

    fun getRandomSortArrayV2(sort: IntArray) {
        val random = Random()
        val length = sort.size
        for (index in 0 until length) {
            val newIndex = random.nextInt(length)
            val tmp = sort[index]
            sort[index] = sort[newIndex]
            sort[newIndex] = tmp
        }
    }
}